/**
* AutoSWF - Cody Cushing. March 26, 2009
* Copyright (c) 2009 Cody Cushing
* No use restrictions. Maintain this notice in all versions. 
**/

package com.autoswf.core
{
	import flash.events.Event;
	
	import flexunit.framework.TestCase;

	public class TransportTest extends TestCase
	{
		public function TransportTest(methodName:String=null)
		{
			super(methodName);
		}
		
		public function testMessage():void
		{
			var transport:Transport = new Transport("id");
			assertEquals("incorrectly rendered message", "id", transport.message);
		}
		
		public function testProposition():void
		{
			var transport:Transport = new Transport("id");
			assertEquals("incorrectly rendered message", "id", transport.proposition);
		}
				
		public function testToString():void
		{
			var transport:Transport = new Transport("id");
			assertEquals("incorrectly printed string", "id", transport.toString());
		}


		public function testMessageWithPath():void
		{
			var transport:Transport = new Transport("path->id");
			assertEquals("incorrectly rendered message", "id", transport.message);
		}

		public function testPropositionWithPath():void
		{
			var transport:Transport = new Transport("path->id");
			assertEquals("incorrectly rendered message", "path", transport.proposition);
		}

		public function testToStringWithPath():void
		{
			var transport:Transport = new Transport("path->id");
			assertEquals("incorrectly printed string", "path->id", transport.toString());
		}
		
		
		/**
		 *  Tests that transport does not stringify the event if it is carrying one. 
		 */		
		public function testMessageWithEvent():void
		{
			var event:Event = new Event(Event.CHANGE);
			var transport:Transport = new Transport(event);
			assertStrictlyEquals("incorrectly rendered message", event, transport.message);
		}
		
		public function testPropositionWithEvent():void
		{
			var event:Event = new Event(Event.CHANGE);
			var transport:Transport = new Transport(event);
			assertEquals("incorrectly rendered message", event, transport.proposition);
		}
		
		public function testToStringWithEvent():void
		{
			var transport:Transport = new Transport(new Event(Event.CHANGE));
			assertEquals("", "change", transport.toString());
		}
		

		/**
		 * Tests that a stringified path and object can be converted to a message with path nodes.
		 */		
		public function testMessageWithEventAndPath():void
		{
			var event:Event = new Event(Event.CHANGE);
			var transport:Transport = new Transport(event, "path");
			assertStrictlyEquals("event not rendered as message", event, transport.message);
		}
		
		public function testPropositionWithEventAndPath():void
		{
			var event:Event = new Event(Event.CHANGE);
			var transport:Transport = new Transport(event, "path");
			assertEquals("event not rendered as message", "path", transport.proposition);
		}		
		
		public function testToStringWithEventAndPath():void
		{
			var transport:Transport = new Transport(new Event(Event.CHANGE), "path");
			assertEquals("", "path->change", transport.toString());
		}

		public function testToStringWithObject():void
		{
			var transport:Transport = new Transport(new Object());
			assertEquals("printed object", "[object Object]", transport.toString());
		}


		/**
		 * Tests that coercing the Transport object to string prints its accreted path as a delimited 
		 * string.
		 */		
		public function testPushPath():void
		{
			var transport:Transport = new Transport("id");
			transport.pushPath(new ChainLink("path"));
			assertEquals("incorrectly printed string", "path->id", transport);
		}
		
		public function testPushPathEvent():void
		{
			var transport:Transport = new Transport(new Event(Event.CHANGE));

			assertTrue("Failed to render top of chain as proposition", "id", transport.proposition is Event);
			
			transport.pushPath(new ChainLink("path"));
			assertEquals("Message accessor didnt return object",  "path", transport.proposition);
		}
	
		/**
		 * Tests that Transport correctly reflects next path idas proposition when adding/removing path nodes. 
		 */		
		public function testPopPaths():void
		{
			var transport:Transport = new Transport(new Event(Event.CHANGE), "path");
			assertEquals("Failed to render top of chain as proposition", "path", transport.proposition);
			
			transport.popPath();
			assertTrue("failed to render top of chain as proposition", transport.proposition is Event);
		}
		
	}
}


